package zhieasy.cn.service.book_market.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhieasy.cn.common.utils.ResultVOUtil;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.book_market.Book;
import zhieasy.cn.model.book_market.BookAndUserBookDTO;
import zhieasy.cn.model.book_market.Seller;
import zhieasy.cn.model.book_market.UserBook;
import zhieasy.cn.model.common.WechatUser;
import zhieasy.cn.persistence.mapper.book.BookMapper;
import zhieasy.cn.persistence.mapper.book.UserBookMapper;
import zhieasy.cn.persistence.mapper.book.UserBookPictureMapper;
import zhieasy.cn.persistence.mapper.common.WeChatUserMapper;
import zhieasy.cn.service.book_market.BookService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangzeya
 * @create 2020-08-12 22:31:00
 */

@Service
@Slf4j
public class BookServiceImpl implements BookService {
    @Autowired
    private UserBookMapper userBookMapper;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private UserBookPictureMapper userBookPictureMapper;
    @Autowired
    private WeChatUserMapper weChatUserMapper;
    /**
     * 匹配图书
     * @param isbn
     * @return
     */
    @Override
    public ResultVO<Book> matchBook(String isbn) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isbn", isbn);
        Book book = bookMapper.selectOne(queryWrapper);
        return ResultVOUtil.success(book);
    }

    /**
     * 发布图书
     * @param userBook
     * @return
     */
    @Override
    public ResultVO<Integer> putBook(UserBook userBook) {
        int insert = userBookMapper.insert(userBook);
        String ub_id = userBook.getUbId();

        return ResultVOUtil.success(ub_id);
    }

    /**
     * 修改已发布图书的信息
     *
     *
     * @param ubid
     * @param userBook
     * @return
     */
    @Override
    public ResultVO<Integer> updateBook(String ubid, UserBook userBook) {
        QueryWrapper<UserBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ub_id",ubid);
        int i = userBookMapper.update(userBook, queryWrapper);
        if (i > 0) {
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error(701, "修改已发布图书失败");
        }
    }

    /**
     * 下架图书
     *
     * @param ub_id
     * @return
     */
    @Override
    public ResultVO<Integer> deleteBook(String ub_id) {
        int i = userBookMapper.deleteById(ub_id);
        if (i > 0) {
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error(111, "下架图书失败");
        }
    }

    /**
     * 查看市场上图书
     *
     * @param faction 出售还是借阅
     * @return
     */
    @Override
    public ResultVO<List<UserBook>> allBooks(String faction, int pageIndex , int pageSize) {
        Page<UserBook> Page = new Page<>(pageIndex,pageSize);
        QueryWrapper<UserBook> wrapper = new QueryWrapper<>();
        wrapper.eq("faction", faction);
        IPage<UserBook> lostAndFindIPage = userBookMapper.selectPage(Page, wrapper);
        List<UserBook> records = lostAndFindIPage.getRecords();
        return ResultVOUtil.success(records);
    }

    /**
     * 查看某个用户的某本书的详细信息（每访问一次点击量+1）
     * @param ub_id
     * @return
     */
    @Override
    public ResultVO<UserBook> singleUserBook(String ub_id) {
        QueryWrapper<UserBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ub_id", ub_id);
        UserBook userBook = userBookMapper.selectOne(queryWrapper);
        userBook.setClicks(userBook.getClicks()+1);
        userBookMapper.updateById(userBook);
        return ResultVOUtil.success(userBook);
    }

    /**
     * 图书推荐
     * @param isbn 图书号
     * @param ubId 图书id
     * @return ResultVO<BookAndUserBookDTO>
     */
    @Override
    public ResultVO<BookAndUserBookDTO> recommendBook(String isbn,String ubId) {
        UserBook userBook = userBookMapper.selectById(ubId);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isbn", isbn);
        Book book = bookMapper.selectOne(queryWrapper);
        BookAndUserBookDTO dto = new BookAndUserBookDTO();
        BeanUtils.copyProperties(userBook,dto);
        BeanUtils.copyProperties(book,dto);
        return ResultVOUtil.success(dto);
    }

    /**
     * 查看某种书的详细信息
     * @param isbn
     * @return
     */

    @Override
    public ResultVO<Book> singleBook(String isbn) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isbn", isbn);
        Book book = bookMapper.selectOne(queryWrapper);
        return ResultVOUtil.success(book);
    }

    /**
     * 查看我发布的图书
     * @param faction 出售还是借阅
     * @param open_id 用户id
     * @return
     */
    @Override
    public ResultVO<List<BookAndUserBookDTO>> myBook(String faction,String open_id , int pageIndex , int pageSize) {
        IPage<UserBook> iPage = new Page<>(pageIndex,pageSize);
        QueryWrapper<UserBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id", open_id);
        queryWrapper.eq("faction", faction);
        List<UserBook> userBooks = (List<UserBook>) userBookMapper.selectPage(iPage,queryWrapper);
        List<BookAndUserBookDTO> dtoArrayList = new ArrayList<>();
        //查询出该用户发布的所有图书，并查询官方图书信息
        if (dtoArrayList.size() > 0) {
            for (UserBook userBook : userBooks) {
                Book book = bookMapper.selectById(userBook.getBookId());
                BookAndUserBookDTO bookAndUserBookDTO = new BookAndUserBookDTO();
                BeanUtils.copyProperties(userBook,bookAndUserBookDTO);
                BeanUtils.copyProperties(book,bookAndUserBookDTO);
                dtoArrayList.add(bookAndUserBookDTO);
            }
        }
        return ResultVOUtil.success(dtoArrayList);

    }
    /**
     * 搜索图书
     * @param book_name
     * @return
     */

    @Override
    public ResultVO<List<Book>> searchBook(String book_name, int pageIndex , int pageSize) {
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("book_name", book_name);
        List<Book> Books = bookMapper.selectList(wrapper);
        return ResultVOUtil.success(Books);
    }

    /**
     * 查看某个分类的图书
     *
     * @param classify 分类
     * @return
     */
    @Override
    public ResultVO<List<Book>> bookOfClass(String classify, int pageIndex , int pageSize) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classify", classify);
        List<Book> books = bookMapper.selectList(queryWrapper);
        return ResultVOUtil.success(books);

    }



    /**
     * 查看所有出售此书的人
     *
     * @param isbn
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public ResultVO allSeller(String isbn, int pageIndex, int pageSize) {
        int sizeBegan = (pageIndex-1) * pageSize ;
        int sizeEnd = (pageIndex-1) * pageSize ;
        String faction = "sell";
        List<UserBook> userBooks = userBookMapper.selectUserBookByFactionAndIsbn(faction, isbn, sizeBegan, sizeEnd);
        return ResultVOUtil.success(userBooks);
    }
    /**
     * 查看所有出租此书的人
     *
     * @param isbn
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public ResultVO allBorrower(String isbn, int pageIndex, int pageSize) {
        int sizeBegan = (pageIndex-1) * pageSize ;
        int sizeEnd = (pageIndex-1) * pageSize ;
        String faction = "borrow";
        List<UserBook> userBooks = userBookMapper.selectUserBookByFactionAndIsbn(faction, isbn, sizeBegan, sizeEnd);
        return ResultVOUtil.success(userBooks);
    }


}
